from server import db
from sqlalchemy import Column, Integer
from . import PERMISSION
from sqlalchemy.dialects.mysql import INTEGER
from server.model import BaseOrm


# 角色路由
class RoleRoute(BaseOrm):
    __tablename__ = 'role_route'
    role_id = Column(INTEGER(unsigned=True), server_default="0", index=True, comment="角色id")
    route_id = Column(INTEGER(unsigned=True), server_default="0", comment="路由id")
    permission = Column(INTEGER(unsigned=True), comment="权限 增:0 删:1 改:2 查:3")

    def set_per(self, per):
        self.permission = per

    def disable_per(self, per):
        self.permission &= per

    def enable_per(self, per):
        self.permission |= per

    def is_allow(self, per):
        return self.permission & per == per

    def to_dict(self, include=None, exclude=None):
        result = super().to_dict(include, exclude)
        result['permission'] = PERMISSION.crud(self.permission)
        return result


db.create_all()
